﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AssistCore;
using System.Threading;

namespace Helper
{
    public partial class GreenwoodHelper : CCWin.Skin_Mac
    {
        public GreenwoodHelper()
        {
            InitializeComponent();
            this.Icon = Properties.HelpResource.app;
        }
        private IntPtr mainHWnd = IntPtr.Zero, smallHwnd = IntPtr.Zero;
        private KeyModifiers runModifyKey = KeyModifiers.None, skillModifyKey = KeyModifiers.None;
        private Keys runHotKey = Keys.None, skillHotKey = Keys.None;
        private Thread greenwoodThread;
        private int matchType, similar = 0;

        private void GreenwoodHelper_Shown(object sender, EventArgs e)
        {
            Win32Core.RegisterHotKey(this.Handle, 100, 0, (int)Keys.Home);
            Win32Core.RegisterHotKey(this.Handle, 200, (int)KeyModifiers.Control, (int)Keys.F10);
            Win32Core.RegisterHotKey(this.Handle, 300, (int)KeyModifiers.Control, (int)Keys.F11);
            var modeDataScoure = new List<object>();
            modeDataScoure.Add(new
            {
                Name = "模式一",
                Value = 1
            });
            modeDataScoure.Add(new
            {
                Name = "模式二",
                Value = 3
            });
            modeDataScoure.Add(new
            {
                Name = "模式三",
                Value = 5
            });
            cb_mode.DataSource = modeDataScoure;
            cb_mode.DisplayMember = "Name";
            cb_mode.ValueMember = "Value";

            var similarDataScoure = new List<object>();
            similarDataScoure.Add(new
            {
                Name = "低",
                Value = 1
            });
            similarDataScoure.Add(new
            {
                Name = "中",
                Value = 2
            });
            similarDataScoure.Add(new
            {
                Name = "高",
                Value = 3
            });
            cb_similar.DataSource = similarDataScoure;
            cb_similar.DisplayMember = "Name";
            cb_similar.ValueMember = "Value";
        }

        private void GreenwoodHelper_FormClosed(object sender, FormClosedEventArgs e)
        {
            Win32Core.UnregisterHotKey(this.Handle, 100);
            Win32Core.UnregisterHotKey(this.Handle, 200);
            Win32Core.UnregisterHotKey(this.Handle, 300);
            if (greenwoodThread != null && greenwoodThread.IsAlive)
            {
                greenwoodThread.Abort();
            }
        }
        protected override void WndProc(ref Message m)
        {
            const int WM_HOTKEY = 0x0312;
            switch (m.Msg)
            {
                case WM_HOTKEY:
                    HandleHotkey(m);
                    break;
            }
            base.WndProc(ref m);
        }
        private void HandleHotkey(Message m)
        {
            switch (m.WParam.ToInt32())
            {
                case 100:
                    {
                        btn_start.PerformClick();
                        break;
                    }
                case 200:
                    {
                        SetMainGameWindow();
                        break;
                    }
                case 300:
                    {
                        SetSmallGameWindow();
                        break;
                    }
            }
        }

        private void txb_autoRun_KeyDown(object sender, KeyEventArgs e)
        {
            KeyChanged(sender, e, ref runModifyKey, ref runHotKey);
        }

        private void txb_skill_KeyDown(object sender, KeyEventArgs e)
        {
            KeyChanged(sender, e, ref skillModifyKey, ref skillHotKey);
        }

        private void btn_start_Click(object sender, EventArgs e)
        {
            if (greenwoodThread != null && greenwoodThread.IsAlive)
            {
                StopGreenwood();
            }
            else
            {
                matchType = int.Parse(cb_mode.SelectedValue.ToString());
                similar = int.Parse(cb_similar.SelectedValue.ToString());
                greenwoodThread = new Thread(StartGreenwood);
                greenwoodThread.Start();
                btn_start.Text = "停止挂机(Home)";
                groupBox1.Enabled = false;
                groupBox2.Enabled = false;
            }
            matchType = int.Parse(cb_mode.SelectedValue.ToString());
            similar = int.Parse(cb_similar.SelectedValue.ToString());
        }

        private void SetMainGameWindow()
        {
            var hWnd = Win32Core.GetForegroundWindow();
            StringBuilder sb = new StringBuilder(128);
            Win32Core.GetWindowText(hWnd, sb, sb.Capacity);
            btn_mainWindow.Text = sb.ToString();
            mainHWnd = hWnd;
            MessageBox.Show("设置主窗口成功", "", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }
        private void SetSmallGameWindow()
        {
            var hWnd = Win32Core.GetForegroundWindow();
            StringBuilder sb = new StringBuilder(128);
            Win32Core.GetWindowText(hWnd, sb, sb.Capacity);
            btn_smallWindow.Text = sb.ToString();
            smallHwnd = hWnd;
            MessageBox.Show("设置小号窗口成功", "", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }
        private void KeyChanged(object sender, KeyEventArgs e, ref KeyModifiers modifyKey, ref Keys hotKey)
        {
            if (e.KeyCode == Keys.ShiftKey || e.KeyCode == Keys.Menu || e.KeyCode == Keys.ControlKey)
            {
                return;
            }
            var key = e.KeyCode.ToString();
            var sb = new StringBuilder();
            if (e.Control)
            {
                sb.Append("Ctrl+");
                modifyKey = modifyKey | KeyModifiers.Control;
            }
            if (e.Shift)
            {
                sb.Append("Shift+");
                modifyKey = modifyKey | KeyModifiers.Shift;
            }
            if (e.Alt)
            {
                sb.Append("Alt+");
                modifyKey = modifyKey | KeyModifiers.Alt;
            }
            sb.Append(key);
            ((TextBox)sender).Text = sb.ToString();
            hotKey = e.KeyCode;
        }
        private void StartGreenwood()
        {
            while (true)
            {
                if (mainHWnd == IntPtr.Zero || smallHwnd == IntPtr.Zero)
                {
                    MessageBox.Show("还未定位游戏窗口，请确保大小号窗口都已成功定位！");
                    StopGreenwood();
                    return;
                }
                if (runHotKey == Keys.None || skillHotKey == Keys.None)
                {
                    MessageBox.Show("还未设置相关键位，请设置自动跟随快捷键和技能快捷键！");
                    StopGreenwood();
                    return;
                }
                Win32Core.ShowWindow(mainHWnd, 2);
                Win32Core.SetForegroundWindow(smallHwnd);
                Thread.Sleep(500);
                var deadPosition = GameAction.FindPropPositon(null, matchType, similar);
                if (deadPosition.HasValue)
                {
                    CDD.DDContext.mov(deadPosition.Value.X, deadPosition.Value.Y);
                    Thread.Sleep(100);
                    CDD.DDContext.btn(1);
                    CDD.DDContext.btn(2);
                    Thread.Sleep(500);
                }
                var buffPositon = GameAction.FindPropPositon(null, matchType, similar);
                if (buffPositon.HasValue)
                {
                    CDD.DDContext.mov(buffPositon.Value.X, buffPositon.Value.Y);
                    Thread.Sleep(100);
                    CDD.DDContext.btn(4);
                    CDD.DDContext.btn(8);
                    Thread.Sleep(100);
                }
                if (runModifyKey != KeyModifiers.None)
                {
                    CDD.DDContext.key((int)KeyCodeMap.ModifyKeyToDD(runModifyKey), 1);
                    Thread.Sleep(100);
                }
                CDD.DDContext.key((int)KeyCodeMap.KeysToDD(runHotKey), 1);
                Thread.Sleep(50);
                CDD.DDContext.key((int)KeyCodeMap.KeysToDD(runHotKey), 2);
                if (runModifyKey != KeyModifiers.None)
                {
                    CDD.DDContext.key((int)KeyCodeMap.ModifyKeyToDD(runModifyKey), 2);
                }
                Thread.Sleep(300);

                Win32Core.ShowWindow(smallHwnd, 2);
                Win32Core.SetForegroundWindow(mainHWnd);
                Thread.Sleep(200);
                if (skillModifyKey != KeyModifiers.None)
                {
                    CDD.DDContext.key((int)KeyCodeMap.ModifyKeyToDD(skillModifyKey), 1);
                    Thread.Sleep(100);
                }
                CDD.DDContext.key((int)KeyCodeMap.KeysToDD(skillHotKey), 1);
                Thread.Sleep(50);
                CDD.DDContext.key((int)KeyCodeMap.KeysToDD(skillHotKey), 2);
                if (skillModifyKey != KeyModifiers.None)
                {
                    CDD.DDContext.key((int)KeyCodeMap.ModifyKeyToDD(skillModifyKey), 1);
                    Thread.Sleep(100);
                }
                Thread.Sleep(3000);

            }
        }
        private void StopGreenwood()
        {
            Action action = () =>
            {
                btn_start.Text = "开始挂机(Home)";
                groupBox1.Enabled = true;
                groupBox2.Enabled = true;
            };
            if (btn_start.InvokeRequired)
            {
                btn_start.Invoke(action);
            }
            else
            {
                action();
            }
            greenwoodThread.Abort();
        }
    }
}
